import os
import sys

import cv2
import numpy as np
import json

def json2img(image_path, json_path):

    image = cv2.imread(image_path)

    with open(json_path, 'r') as f:
      annotations = json.load(f)
      for obj in annotations["shapes"]:
          label = obj['label']
          points = obj['points']
          points = np.array(points, dtype=np.int32)
          points = points.reshape((-1, 1, 2))
          cv2.fillPoly(image, [points], color=(0, 255, 0))
          cv2.putText(image,label,tuple(points[0][0]),cv2.FONT_HERSHEY_PLAIN,1,(255,0,0),thickness=2)
    cv2.imshow('image',image)
    cv2.waitKey(0)


def json2imglabels(image_path):
    labels = ['tv', 'phone', 'battery', 'clothes', 'seat', 'people', 'Keyboard']
    for json_path in os.listdir(image_path):
        if json_path.endswith('.json'):
            json_path = os.path.join(image_path, json_path)
            with open(json_path, 'r') as f:
                annotations = json.load(f)
                for obj in annotations["shapes"]:
                    label = obj['label']
                    if label not in labels:
                        print(json_path)


if __name__ == '__main__':
    # image_path = 'C:\Al\Software\AI_Model\Project\yolov5\yolov5\my_data\office_data'
    img_path = r'C:\Al\Software\AI_Model\Project\yolov5\yolov5\my_data\office_data\frame_65.jpg'
    json_path = r'C:\Al\Software\AI_Model\Project\yolov5\yolov5\my_data\office_data\frame_65.json'
    # json2imglabels(image_path)
    json2img(img_path, json_path)

